package inftel.android;

import inftel.android.*;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class PantallaInicio extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.inicio);

		// Localizar los controles
		final EditText txtNombre = (EditText) findViewById(R.id.EditTextNombre);
		final EditText txtPass = (EditText) findViewById(R.id.EditTextPass);

		final Button btnAceptar = (Button) findViewById(R.id.BtnAceptar);
		btnAceptar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Creamos el Intent
				Intent intent = new Intent(PantallaInicio.this, GoogleMaps.class);

				// Creamos la informaci�n a pasar entre actividades
				Bundle b = new Bundle();

				String name = txtNombre.getText().toString();
				String pass = txtPass.getText().toString();

				//sString servletURL = "http://10.0.2.2:8080/CentralEnBuscaLaBallena-war/LoginServlet";
				String servletURL = "http://192.168.183.68:8080/CentralEnBuscaLaBallena-war/LoginServlet";
				
				try {

					URL ob = new URL(servletURL + "?usuario=" + name + "&pass="	+ pass);

					URLConnection conn = ob.openConnection();

					conn.setDoInput(true);
					conn.setDoOutput(true);
					InputStream in = conn.getInputStream();
					DataInputStream dos = new DataInputStream(in);
					String leido = dos.readLine();

					b.putString("NOMBRE", leido);

					Log.i("-------Nombre", txtNombre.getText().toString());
					Log.i("-------Pass", leido);

					// A�adimos la informaci�n al intent
					intent.putExtras(b);

					// Iniciamos la nueva actividad
					startActivity(intent);

				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		
		
		//boton registrar
		
		final Button btnRegistrar = (Button) findViewById(R.id.BtnRegistrar);
		btnRegistrar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Creamos el Intent
				Intent intent = new Intent(PantallaInicio.this, GoogleMaps.class);

				startActivity(intent);

			}
		});
		
		
		
		
	}
}